import logging
import requests
import os
import subprocess
from django.conf import settings    # setting.py添加的的配置信息
# 项目projects  url
url = settings.HARBOR_URL + '/api/v2.0/projects/'
 
# 存储项目名称
project_name = []
 
# 存储镜像,只有镜像路径
image_list = []
 
# 封装之后里面有镜像路径和tag
image_new_list = []
 
# 日志路径
current_path = os.path.dirname(os.path.abspath(__file__))
log_filename = current_path + '/' + 'info.log'
logging.basicConfig(filename=log_filename,
                    level=logging.INFO,
                    format='%(asctime)s \"%(filename)s\"[line:%(lineno)d] %(levelname)s %(message)s',
                    datefmt="%Y-%m-%d %H:%M:%S",
                    filemode='a')
 
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0"
}
 
 
def get_project_name():
    response_project = requests.get(url, headers=headers, auth=(settings.HARBOR_ACCOUT_NAME, settings.HARBOR_ACCOUT_PASSWORD), verify=False)
    # 返回结果是一个列表里面包含了很多字典
    response_list = response_project.json()
    # print(response_project.status_code)
    for dic in response_list:
        dicts = {}
        for name in dic:
            if name == 'name':
                dicts['name'] = dic[name]
            if name == 'repo_count':
                dicts['page'] = dic[name] // 1000 + 1
        project_name.append(dicts)
    # print(project_name)
    return project_name
 
 
# 获取镜像完成路径，传输参数：pr_name 是 project_name 是一个列表，返回镜像列表
def get_image_name(pr_name):
    for pro_name in pr_name:
        url_repositories = url + pro_name['name'] + '/repositories'
        params = {
            "page": pro_name['page'],
            "page_size": 100
        }
        response_repositories = requests.get(url_repositories, params=params, headers=headers,
                                             auth=(settings.HARBOR_ACCOUT_NAME, settings.HARBOR_ACCOUT_PASSWORD),
                                             verify=False)
        list_response = response_repositories.json()
        # print(response_repositories.status_code)
        if len(list_response) == 0:
            continue
        else:
            for repositories_name in list_response:
                # print(repositories_name)
                image_list.append(repositories_name['name'])
        return image_list
 
 
pro_name = get_project_name()
# print(pro_name)
get_image_name(pro_name)
 
 
print(image_list)
 
 
# get_tag 函数返回一个列表包含字典，字典里面是name 和 tag
def get_tag(images_list):
    list1 = []
    for names in images_list:
        index = names.find('/')
        p_name = names[:index]
        reps_name = names[index + 1:].replace('/', '%252F')
        tag_url = url + p_name + '/repositories/' + reps_name + '/artifacts'
        re = requests.get(tag_url, auth=(settings.HARBOR_ACCOUT_NAME, settings.HARBOR_ACCOUT_PASSWORD))
        for di in re.json():
            dict_image = {'tag': []}
            for tag in di['tags']:
                dict_image['name'] = names
                dict_image['tag'].append(tag['name'])
            for tag in dict_image['tag']:
                image_new_list.append(dict_image['name'] + ':' + tag)
    return image_new_list
 
 
ll = get_tag(image_list)
print(ll)
 
 
# compare_list_with_file函数作用比较镜像列表和文件内容的不同地方，different_content存储不同的内容
def compare_list_with_file(images, file_path):
    # 读取文件内容
    with open(file_path, 'r') as file:
        file_content = file.read()
    different_content = []
    # 比较列表中的每个元素
    if images not in file_content:
        different_content.append(images)
 
    # 输出不同的内容
    return different_content
 
 
# 函数 sync 进行同步镜像，参数为镜像名称:tag 和 日志路径
def sync(image, logs_done,logs_not_done):
    destination_address = '172.20.216.103/'
    source_address = '172.20.216.103/'
    try:
        os.system('docker pull ' + source_address + image)
        os.system('docker tag ' + source_address + image + ' ' + destination_address + image)
        print('docker tag ' + source_address + image + ' ' + destination_address + image)
        os.system('docker push ' + destination_address + image)
        push = 'docker push ' + destination_address + image
        result_push = subprocess.run(push, shell=True)
        print(result_push.returncode)
        if result_push.returncode == 0:
            f = open(logs_done, 'a')
            f.write(image + '\n')
            f.close()
        else:
            f = open(logs_not_done, 'a')
            f.write(image + '\n')
            f.close()
        print('docker push ' + destination_address + image)
        os.system('docker rmi ' + destination_address + image)
        os.system('docker rmi ' + source_address + image)
        logging.info(image)
    except Exception as err:
        logging.error(err)
 
 
# for images in image_new_list:
#     current_path = os.path.dirname(os.path.abspath(__file__))
#     log_done = current_path + '/' + 'done.log'
#     logs_not_done = current_path + '/' + 'not_done.log'
#     try:
#         # if os.path.getsize(log_done) == 0:
#         #     logging.info('正在进行全量同步！')
#         #     sync(images, log_done)
#         # else:
#         different = compare_list_with_file(images, log_done)
#         for diff in different:
#             sync(diff, log_done,logs_not_done)
#     except Exception as err:
#         logging.error(err)